#include <string.h>
#include <stdio.h>
#include "air105.h"
#include "delay.h"

void UART_Configuration(void); // 串口初始化

int main(void)
{
	SYSCTRL_ClocksTypeDef clocks;
	SYSCTRL_PLLConfig(SYSCTRL_PLL_204MHz);												 // 设置PLL时钟
	SYSCTRL_GetClocksFreq(&clocks);														 // 获取时钟频率
	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_GPIO, ENABLE); // 使能UART0和GPIO时钟
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0, ENABLE);							 // 复位UART0

	UART_Configuration(); // 串口初始化
	Delay_Init();		  // 延时初始化
	printf("CPU_Frequency:%d,\nHCLK_Frequency:%d,\nPCLK_Frequency:%d,\nPLL_Frequency:%d\n\r", clocks.CPU_Frequency / 1000000, clocks.HCLK_Frequency / 1000000, clocks.PCLK_Frequency / 1000000, clocks.PLL_Frequency / 1000000);
	Delay_ms(100);

	while (1)
	{
		printf("hello world\n\r");
		Delay_ms(1000);
	}
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;

	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 | GPIO_Pin_1, GPIO_Remap_0); // PA0,PA1复用为UART0_TXD,UART0_RXD,GPIO_Remap_0这个参数看芯片手册复用功能表

	UART_InitStructure.UART_BaudRate = 115200;				 // 波特率
	UART_InitStructure.UART_WordLength = UART_WordLength_8b; // 数据位
	UART_InitStructure.UART_StopBits = UART_StopBits_1;		 // 停止位
	UART_InitStructure.UART_Parity = UART_Parity_No;		 // 校验位

	UART_Init(UART0, &UART_InitStructure); // 初始化UART0
}

// 重定向c库函数printf到UART
int SER_PutChar(int ch)
{
	while (!UART_IsTXEmpty(UART0))
		;
	UART_SendData(UART0, (uint8_t)ch);

	return ch;
}

int fputc(int c, FILE *f)
{
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	if (c == '\n')
	{
		SER_PutChar('\r');
	}
	return (SER_PutChar(c));
}
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
#ifdef USE_FULL_ASSERT

/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif
